package com.ahzy.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ahzy.po.Comment;
import com.ahzy.po.Notice;
import com.ahzy.service.CommentService;
import com.ahzy.vo.CommentAndUser;
import com.ahzy.vo.NoticeAndUser;
import com.ahzy.service.CommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@RestController
@RequestMapping("/comment")
@Slf4j
@Api
public class CommentContriller {

	@Autowired
	CommentService commentService;

	@ApiOperation(value = "获取列表分页数据")
	@GetMapping("/data")
	public Map<String, Object> data(@RequestParam(value = "page", required = false, defaultValue = "1000") int page,
			@RequestParam(value = "limit", required = false, defaultValue = "10") int limit,
			@RequestParam(value = "nid", required = false, defaultValue = "0") int nid) {
		
		Example commentExample = new Example(Comment.class);
		Criteria commentCriteria = commentExample.createCriteria();
		//添加条件查询
		if(nid!=0) {
			commentCriteria.andEqualTo("nid",nid);
		}
		PageInfo<Comment> rolePageInfo = commentService.selectByExamplePage(page, limit, commentExample);
		Map<String, Object> map = new HashMap<>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("total", rolePageInfo.getTotal());
		map.put("data", rolePageInfo.getList());
		return map;
	}

	@ApiOperation(value = "获取列表分页数据")
	@GetMapping("/commentAndUserdata")
	public Map<String, Object> noticeAndUserdata(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
			@RequestParam(value = "limit", required = false, defaultValue = "1000") int limit,
			@RequestParam(value = "nid", required = false, defaultValue = "0") int nid) {
		Map<String, Object> mapData = new HashMap<>();
		PageHelper.startPage(page, limit);
		PageInfo<CommentAndUser> CommentAndUserPageInfo = new PageInfo<>(commentService.selectByUserAndCommentExamplePage(nid));
		mapData.put("code", 0);
		mapData.put("msg", "");
		mapData.put("state", true);
		mapData.put("data", CommentAndUserPageInfo);
		return mapData;
	}
	
	
	@ApiOperation(value = "有主键则修改，否则新增")
	@GetMapping("/save")
	public Map<String, Object> save(@Validated  Comment comment) {
		System.out.println(comment);
		Map<String, Object> mapData = new HashMap<>();
		try {
			if (comment.getId() == null) {
				// 新增操作
				comment.setDateline(new Date());
				commentService.insertWithoutPrimaryKey(comment);
				mapData.put("state", true);
			} else {
				// 修改操作
				commentService.updateByPrimaryKeySelective(comment);
				mapData.put("state", true);
			}
		} catch (Exception e) {
			log.error("{}", e.getMessage());
		}
		return mapData;
	}

	@ApiOperation(value = "通过id删除管理员信息")
	@DeleteMapping("/remove/{id}")
	public Map<String, Object> delete(@PathVariable Integer id) {
		Map<String, Object> mapData = new HashMap<>();
		try {
			commentService.deleteByPrimaryKey(id);
			mapData.put("state", true);
		} catch (Exception e) {
			mapData.put("state", false);
		}
		mapData.put("state", true);
		return mapData;
	}

	@ApiOperation(value = "通过id查看管理员信息")
	@GetMapping("/detail/{id}")
	public Map<String, Object> detail(@PathVariable Integer id) {
		Map<String, Object> map = new HashMap<>();
		Comment Comment = commentService.selectByPrimaryKey(id);
		map.put("data", Comment);
		return map;
	}

}
